package cn.myeasyai;

import cn.myeasyai.config.FileConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import jakarta.annotation.Resource;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.nio.charset.Charset;
import java.util.List;
import java.util.Objects;

/**
 * @author fushoujiang
 */
@Component
public class WebMvcConfigurer extends WebMvcConfigurationSupport {


    @Resource
    FileConfig fileConfig;
    /**
     * 消息转换器自定义配置，向当前的Http消息转换器列表增加阿里云的FastJson消息转换器
     *
     * @param converters 当前的Http消息转换器列表对象
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //解决中文系列化后成乱码
        fastJsonConfig.setCharset(Charset.forName("GB2312"));
        fastJsonConverter.setFastJsonConfig(fastJsonConfig);
        //converters.add(fastJsonConverter);//这会让fastJsonConverter排在消息转换器管道列表的最后，可能会轮不到它处理消息转换
        //要显示指明将fastJsonConverter排在消息转换器管道列表的首位
        converters.add(0, fastJsonConverter);
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * 访问路径：http://localhost:8086/image/2022001.png
         * "/image/**" 为前端URL访问路径
         * "file:" + bmpPath 是本地磁盘映射
         */
        final FileConfig.Local local = fileConfig.getLocal();
        if (Objects.nonNull(local)){
            registry.addResourceHandler("/image/**").addResourceLocations("file:" + local.getPath(),"file:" +"/Users/yangjie/Downloads/success/");
        }
    }

}
